/*
 * Copyright 2008 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.sis.main.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.i18n.client.Constants;
import com.sis.main.client.ContentWidget;
import com.sis.main.client.SISAnnotations.ShowcaseData;
import com.sis.main.client.SISAnnotations.ShowcaseSource;
import com.sis.main.client.SISAnnotations.ShowcaseStyle;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Example file.
 */
@ShowcaseStyle(".gwt-CheckBox")
public class CwViewSecurities extends ContentWidget {
	
	  private static final int REFRESH_INTERVAL = 5000 * 12 * 3; //every 1 minute
	  private PopupLogin popupLogin;
	  private HorizontalPanel hpanelInsertSecurity = new HorizontalPanel();
	  private VerticalPanel vpanelMain = new VerticalPanel();
	  private TextBox textboxInsertSecurity = new TextBox();
	  private Button buttonInsertSecurity = new Button("Insert");
	  private Label labelLastUpdateDateTime = new Label();
	  private TableModelSecurities tablemodelSecurities = new TableModelSecurities();

	
  /**
   * The constants used in this Content Widget.
   */
  @ShowcaseSource
  public static interface CwConstants extends Constants,
      ContentWidget.CwConstants {
	  
    String cwViewSecuritiesDescription();

    String cwViewSecuritiesName();
  }
  

  /**
   * An instance of the constants.
   */
  @ShowcaseData
  private CwConstants constants;
 

  /**
   * Constructor.
   * 
   * @param constants the constants
   */
  public CwViewSecurities(CwConstants constants) {
    super(constants);
    this.constants = constants;
  }

  @Override
  public String getDescription() {
    return constants.cwViewSecuritiesDescription();
  }

  @Override
  public String getName() {
    return constants.cwViewSecuritiesName();
  }

  /**
   * Initialize this example.
   */
  @ShowcaseSource
  @Override
  public Widget onInitialize() {
	  popupLogin = new PopupLogin(this);
	  return loadViewSecurities(); 
  }//end onInit

  protected Widget loadViewSecurities() {
	  //set visible to false until after PopupLogin setsVisible(true);
	  	this.setVisible(false);
	    // Assemble Add Stock panel.
	    hpanelInsertSecurity.add(textboxInsertSecurity);
	    hpanelInsertSecurity.add(buttonInsertSecurity);

	    // Assemble Main panel.
	    vpanelMain.add(tablemodelSecurities.getSecuritiesFlexTable());
	    vpanelMain.add(hpanelInsertSecurity);
	    vpanelMain.add(labelLastUpdateDateTime);
	    
	    // Move cursor focus to the input box.
	    textboxInsertSecurity.setFocus(true);
	    
	    // Setup timer to refresh list automatically.
	    Timer timerUpdateSecurities = new Timer() {
	      @Override
	      public void run() {
	    	  if (! popupLogin.getDataLogin().isLoggedIn()){
	    		  return;
	    	  }
	    	  if ( popupLogin.getDataLogin().isLoggedIn()){
	    		  tablemodelSecurities.getWatchList();
	    		  this.scheduleRepeating(REFRESH_INTERVAL);  //every minute
	    	  }
	      }//end run()
	    }; //end timer class
	    timerUpdateSecurities.scheduleRepeating(1000);//every second
	    
	    getTableModelSecurities().getWatchList();
	    
	    // Listen for mouse events on the Add button.
	    buttonInsertSecurity.addClickHandler(new ClickHandler() {
	      public void onClick(ClickEvent event) {
	    	  insertSecurity();
	      }
	    });

	    // Listen for keyboard events in the input box.
	    textboxInsertSecurity.addKeyPressHandler(new KeyPressHandler() {
	      public void onKeyPress(KeyPressEvent event) {
	        if (event.getCharCode() == KeyCodes.KEY_ENTER) {
	          insertSecurity();
	        }
	      }
	    });


  // Return the panel
  return vpanelMain;
	  
  }
  

	protected void insertSecurity() {
	    final String stringSecuritySymbol = textboxInsertSecurity.getText().toUpperCase().trim();
	    textboxInsertSecurity.setFocus(true);

	    // Stock code must be between 1 and 10 chars that are numbers, letters, or dots.
	    if (!stringSecuritySymbol.matches("^[0-9A-Z\\.]{1,10}$")) {
	      Window.alert("'" + stringSecuritySymbol + "' is not a valid Security Symbol.");
	      textboxInsertSecurity.selectAll();
	      return;
	    }

	    textboxInsertSecurity.setText("");
	    
	    DataSecurity dataSecurity = new DataSecurity();
	    dataSecurity.setSymbol(stringSecuritySymbol);
	    tablemodelSecurities.insertIntoFlexTableWatchList(dataSecurity);
	    //save to database, must get row id from previous call first
	    tablemodelSecurities.saveSecurity(dataSecurity);
	    
	    
	    // Get the stock price.
	    //updateSecurities();

	}//end insertSecurity
	
	public TableModelSecurities getTableModelSecurities() {
		return tablemodelSecurities;
	}

  
}// end class
